/**************************************************************************************
*
*	Copyright (C) 2009 - 2012 Brent Andrew O. Chua - See LICENSE.txt for more info.
*		
*	Filename	-	Main.cpp
*
*	Comments	-	
*
**************************************************************************************/

#include "../Include/PulseSTD.h"
#include "../Include/Application.h"

INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nShowCmd );

namespace Pulse
{
	INT PulseMain( LPSTR lpCmdLine )
	{
		// Call Pulse Window application entry-point
		EErrorCode::Type retCode;
		Engine *pEngine = Engine::GetInstance();
		Application *pApp = Application::GetInstance();
		String cmdLine( lpCmdLine );
		
		retCode = pEngine->Initialize();
		if ( retCode != EErrorCode::OKAY )
		{
			PSX_PushError( "Failed to initialize Pulse Engine." );
			return -1;
		}

		PSX_PushLog3( "----------BEGIN----------" );
		PSX_PushLog3( "Successfully initialized Pulse Engine" );

		retCode = pApp->Initialize();
		if ( retCode != EErrorCode::OKAY )
		{
			PSX_PushError( "Failed to initialize Pulse Application." );
			return -1;
		}

		PSX_PushLog3( "Successfully initialized Pulse Application" );

		retCode = pApp->Run( cmdLine.GetBuffer() );
		if ( retCode != EErrorCode::OKAY )
		{
			PSX_PushError( "Failed to run Pulse Application." );
		}

		PSX_PushLog3( "Successfully ran Pulse Application." );
		PSX_PushLog3( "Shutting down Pulse Application." );
		
		pApp->ShutDown();
		pApp->DeleteInstance();
		pApp = PSX_NULL;

		PSX_PushLog3( "Shutting down Pulse engine." );
		PSX_PushLog3( "----------END----------" );

		pEngine->Shutdown();
		pEngine->DeleteInstance();
		pEngine = PSX_NULL;

		//PSX_PushLog3( "Shut down complete." );

		return 0;
	}
}

// Windows window application entry-point
INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nShowCmd )
{
	return Pulse::PulseMain( lpCmdLine );
}
